package com.utc.cards.table.jade.agent.rulesAgent;

import jade.core.AID;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.utc.cards.Constants;
import com.utc.cards.common.jade.Mapper;
import com.utc.cards.model.game.IRules;

public class DeterminateWinnerCurrentFoldBehaviour extends OneShotBehaviour
{

    private static final long serialVersionUID = 6630252068292867140L;
    private static Logger log = LoggerFactory.getLogger(DeterminateWinnerCurrentFoldBehaviour.class);

    private RulesAgent agent;
    private IRules rules;

    public DeterminateWinnerCurrentFoldBehaviour(RulesAgent rulesAgent)
    {
        super(rulesAgent);
        this.agent = rulesAgent;
        this.rules = agent.getModel().getGame().getRules();
    }

    @Override
    public void action()
    {
        log.debug("Computing winner ...");
        ACLMessage message = new ACLMessage(ACLMessage.INFORM);
        try
        {
            message.setConversationId("winner-player");
            message.setContent(Mapper.getObjectMapper().writeValueAsString(rules.determinateWinnerCurrentFold().getName()));
            message.addReceiver(new AID(Constants.CARDS_HOST_GAME_AGENT_NAME, AID.ISLOCALNAME));
            agent.send(message);
        }
        catch (JsonProcessingException e)
        {
            log.error("Error while mapping String");
            e.printStackTrace();
        }
        log.debug("Computing winner DONE");

    }
}
